package com.aerozhonghuan.logisticsmall.utils;

import android.text.TextUtils;
import android.util.Base64;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

public class JsonUtil {
    public static final String TAG = "JsonUtil.java";

    private static Gson gson = null;

    static {
        if (gson == null) {
            gson = new Gson();
        }
    }

    public static <T> T toBean(String gsonString, Class<T> cls) {
        T t = null;
        if (gson != null) {
            t = gson.fromJson(gsonString, cls);
        }
        return t;
    }

    /**
     * 解析集合
     * @param responseBody
     * @param typeToken
     * @param <T>
     * @return
     */
    public static <T> List<T> parseList(String responseBody, TypeToken<ArrayList<T>> typeToken) {
        String json = decrypt(responseBody);
        if (TextUtils.isEmpty(responseBody)) {
            return null;
        }
        Gson gson = new Gson();
        Type listType = typeToken.getType();
        List<T> beans = gson.fromJson(responseBody, listType);
        return beans;
    }

    public static <T> List<T> parsesList(String responseBody, TypeToken<ArrayList<T>> typeToken) {
        if (TextUtils.isEmpty(responseBody)) {
            return null;
        }
        Gson gson = new Gson();
        Type listType = typeToken.getType();
        List<T> beans = gson.fromJson(responseBody, listType);
        return beans;
    }

    /**
     * 解析Base64对象
     * @param responseBody
     * @param typeToken
     * @param <T>
     * @return
     */
    public static <T> T parseObject(String responseBody, TypeToken<T> typeToken) {
        String json = decrypt(responseBody);
        if (TextUtils.isEmpty(json)) {
            return null;
        }
        Gson gson = new Gson();
        Type type = typeToken.getType();
        T bean = gson.fromJson(json, type);
        return bean;
    }

    /**
     * 解析json数据
     *
     * @param responseBody
     * @param typeToken
     * @return
     */
    public static <T> T parsesObject(String responseBody, TypeToken<T> typeToken) {
        if (TextUtils.isEmpty(responseBody)) {
            return null;
        }
        Gson gson = new Gson();
        Type type = typeToken.getType();
        T bean = gson.fromJson(responseBody, type);
        return bean;
    }

    /**
     * 将对象转为json
     *
     * @param obj
     * @return
     */
    public static String toJson(Object obj) {
        if (null != obj) {
            Gson gson = new Gson();
            return gson.toJson(obj);
        } else {
            return "";
        }
    }

    /**
     * 解密json数据
     *
     * @param json
     * @return
     */
    public static String decrypt(String json) {
        if (json == null) {
            return null;
        }
        int len = json.length();
        int pos = len - 7;
        StringBuilder sb = new StringBuilder(json);
        for (int i = 0; i < 3; i++) {
            sb = sb.reverse();
            sb = new StringBuilder(sb.substring(pos, len) + sb.substring(0, pos));
        }
        byte[] result = Base64.decode(sb.toString(), Base64.DEFAULT);
        String res = new String(result);
        return res;
    }

}
